import React, { Component } from 'react';
import css from './index.less';
import FilterSearch from '@/components/FilterSearch';
import TableList from '@/components/TableList';
import EditModal from '@/components/EditModal';
import { Button, Divider } from 'antd';
import { connect } from 'dva';
import { timeFormat } from '@/utils/common';
import { Link } from 'react-router-dom';
import Status from '@/pages/components/Status';
import AuthButton from '@/components/AuthButton';
type State = {};
type Props = {
  dispatch: Function;
};
class Department extends Component<Props> {
  state: State = {};
  table = null;

  filters = [
    {
      key: 'buildType',
      type: 'select',
      label: '项目性质',
      options: this.props.dict.buildType,
    },
    {
      key: 'serviceTypeUuid',
      type: 'select',
      label: '项目类型',
      asyncopt: {
        url: '/api/data/v1/getDataList',
        data: {
          code: 'service',
        },
      },
    },
    {
      key: 'professionDataUuid',
      type: 'select',
      label: '行业领域',
      asyncopt: {
        url: '/api/data/v1/getDataList',
        data: {
          code: 'profession',
        },
      },
      span: 12,
      placeholder: '请选择',
      rules: [{ required: true }],
    },
    {
      key: 'projectStatus',
      type: 'select',
      label: '项目状态',
      options: this.props.dict.projectStatus,
    },

    {
      key: 'no',
      type: 'input',
      label: '项目ID',
    },
    {
      key: 'name',
      type: 'input',
      label: '项目名称',
    },
    {
      key: 'code',
      type: 'input',
      label: '项目代码',
    },
  ];
  columns = [
    { title: '项目ID', dataIndex: 'no' },
    {
      title: '项目名称',
      dataIndex: 'name',
      render: (text, data) => (
        <Button type="link" onClick={() => this.edit(data, true)}>
          {text}
        </Button>
      ),
    },
    { title: '项目状态', dataIndex: 'projectStatus' },
    { title: '业主单位', dataIndex: ['ownerOrgUnit', 'name'] },
    { title: '项目代码', dataIndex: 'code' },
    { title: '项目性质', dataIndex: 'buildType' },
    { title: '项目类型', dataIndex: ['serviceTypeData', 'display'] },
    { title: '行业领域', dataIndex: ['professionData', 'display'] },

    {
      title: '操作',
      width: 325,
      render: (text, data) => (
        <span key={text}>
          {~['建设中', '使用中'].indexOf(data.projectStatus) ? (
            <AuthButton
              onClick={() => this.handler(data, 'close')}
              role="edit_accept_info"
            >
              关闭
            </AuthButton>
          ) : (
            ''
          )}
          {data.projectStatus == '已关闭' ? (
            <AuthButton
              onClick={() => this.handler(data, 'restart')}
              role="del_accept_info"
            >
              重启
            </AuthButton>
          ) : (
            ''
          )}
        </span>
      ),
    },
  ];

  handler = (data, type) => {
    const url =
      type == 'close'
        ? '/api/project/store/v1/close'
        : '/api/project/store/v1/restart';
    const title = type == 'close' ? '关闭' : '重启';
    this.props
      .dispatch({
        type: 'base/handler',
        payload: {
          type: `${title}项目`,
          url,
          data: {
            uuidList: [data.uuid],
          },
        },
      })
      .then(() => {
        this.table.fetch();
      });
  };
  edit = (data: any, readOnly: boolean) => {
    const url = `projectCollect/projectFileCollect/edit?id=${data.uuid ||
      ''}&readOnly=${readOnly || ''}&fromType=1`;
    this.props.dispatch({
      type: '$router/forward',
      payload: {
        url,
      },
    });
  };

  render() {
    return (
      <div>
        <FilterSearch
          filters={this.filters}
          onSearch={val => this.table.fetch(val)}
        ></FilterSearch>
        <TableList
          post
          rowKey="id"
          onRef={c => {
            this.table = c;
          }}
          columns={this.columns}
          source="/api/project/store/v1/project/query"
        />
      </div>
    );
  }
}

export default connect(state => state.base)(Department);
